/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pmi.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pmi.dao.implement.pasienimplement;
import pmi.dao.interf.pasieninterface;
import pmi.dao.model.pasien;
import java.util.List;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author Deny Prasetyo
 */
public class pasienservlet extends HttpServlet {

    private pasieninterface dao;
    public pasienservlet() {
        dao= new pasienimplement();
    }

    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<pasien> list=dao.getAll();
        request.setAttribute("list", list);
        RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/pasien/list.jsp");
        dispatcher.forward(request, response);
        //response.getWriter().print(list.get(0).getNama());
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action=request.getParameter("action");
        if(action!=null){
            if(action.equalsIgnoreCase("insert")){
                RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/pasien/form.jsp");
                dispatcher.forward(request, response);
            }else if(action.equalsIgnoreCase("delete")){
                String id=request.getParameter("id");
                dao.delete(id);
                processRequest(request, response);
            }else if(action.equalsIgnoreCase("update")){
                
                RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/pasien/update.jsp");
                String id=request.getParameter("id");
                pasien p = new pasienimplement().getById(id);
                request.setAttribute("pasien", p);
                dispatcher.forward(request, response);
            }
        }else{
            processRequest(request, response);
        }
        processRequest(request, response);
        
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        pasien p=new pasien();
        p.setTanggal(request.getParameter("tanggal"));
        p.setId(request.getParameter("id"));
        p.setNama(request.getParameter("nama"));
        p.setAlamat(request.getParameter("alamat"));
        p.setTelp(request.getParameter("telp"));
        p.setUmur(request.getParameter("umur"));
        p.setGoldar(request.getParameter("gol"));
        p.setKelamin(request.getParameter("kelamin"));
        p.setBerat(request.getParameter("berat"));
        p.setTinggi(request.getParameter("tinggi"));
        p.setKeterangan(request.getParameter("keterangan"));
        String action=request.getParameter("action");
        if(action!=null){
            if(action.equalsIgnoreCase("insert")){
                dao.insert(p);
            
            }else if(action.equalsIgnoreCase("update")){
                dao.update("id", p);
            }
        }else{
            processRequest(request, response);
        }
        
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
